import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;

import java.util.concurrent.ConcurrentHashMap;
/**
 * Created by huangxiaolu on 2015/2/13.
 */
public class Distributor {
    public static void main(String[] args) throws IOException, InterruptedException {
        publish();
    }

    /**
     * @throws java.io.IOException
     * @throws InterruptedException
     */
    private static void send()
            throws java.io.IOException, InterruptedException {

        ConcurrentHashMap hashMap=new ConcurrentHashMap();


        String exchangeName = "hello";
        // write your code here
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(exchangeName, "fanout");
        String message = "Hi J";
        for (int i = 0; i < 1000000; i++) {
            channel.basicPublish(exchangeName, "", null, message.getBytes());
        }
        channel.close();
        connection.close();
        System.out.println("[x] sent '" + message + "'");
        System.in.read();
    }

    private static void publish() throws IOException, InterruptedException {

        FileReader reader = new FileReader("D:/Program Files/DebugDiag/Logs/DbgSVC_Date__11_13_2013__Time_05_53_09PM__517__Log.txt");
        String message = readToEnd(reader);
        String[] args = new String[]{"info", "info log message"};
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        String exchangeName = "logs-routed";
        channel.exchangeDeclare(exchangeName, "direct");
        StringBuilder sb =new StringBuilder();
        sb.append(args[1]);
        sb.append(":");
        sb.append(message);
        message = sb.toString();
//        StringWriter sw = new StringWriter();
//        sw.append(args[1]);
//        sw.append(":");
//        sw.append(message);
//        message = sw.toString();
        int j = 0;
        while (true) {
            for (int i = 0; i < 100; i++) {
                int id = j * 10 + i;
                String tmpMessage = id + message;
                byte[] body = tmpMessage.getBytes();
                channel.basicPublish(exchangeName, args[0], null, body);
                System.out.format("[x] sent %s %s%n", args[0], (args[1] + id));
            }
            System.out.print("press any key to publish next batch");
            System.in.read();
            j++;
        }
    }

    private static String readToEnd(FileReader reader) throws IOException {
        StringWriter writer = new StringWriter();
        char[] arr = new char[1024];
        int len = reader.read(arr);
        while (len > 0) {
            writer.write(arr, 0, len);
            len = reader.read(arr);
        }
        return writer.toString();
    }
}
